/**
 * Render Bender - A Flash-based chained Pixel Bender kernel previewer
 * Copyright (C) 2009  David Lenaerts
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author information:
 * David Lenaerts
 * url: http://www.derschmale.com
 * mail: david.lenaerts@gmail.com
 * 
 */
 
package com.derschmale.renderBender.model
{
	import com.derschmale.renderBender.ApplicationFacade;
	import com.derschmale.renderBender.model.vo.AssetDefinition;
	import com.derschmale.renderBender.model.vo.KernelDefinition;
	import com.derschmale.renderBender.model.vo.KernelSequence;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	
	import org.puremvc.as3.patterns.proxy.Proxy;

	public class KernelSequenceProxy extends Proxy
	{
		public static const NAME : String = "KernelSequenceProxy";
		
		public function KernelSequenceProxy()
		{
			// temps for disappearing imports:
			new AssetDefinition();
			new KernelDefinition();
			
			super(NAME, new KernelSequence());
			kernelSequence.addEventListener(KernelSequence.READY, onSequenceReady);
		}
		
		public function setBitmapDataAsset(id : String, bitmapData : BitmapData) : void
		{
			kernelSequence.setBitmapDataAsset(id, bitmapData);
			sendNotification(ApplicationFacade.UPDATE_ASSET, bitmapData, id);
		}
		
		public function resetAssets() : void
		{
			kernelSequence.reset();
		}
		
		public function get synchronous() : Boolean
		{
			return kernelSequence.forceWait;
		}
		
		public function set synchronous(value : Boolean) : void
		{
			kernelSequence.forceWait = value;
		}
		
		public function execute() : void
		{
			if (kernelSequence.isReady) kernelSequence.execute();
		}
		
		public function get kernelSequence() : KernelSequence
		{
			return data as KernelSequence;
		}
		
		public function createSequence(assetDefs : Vector.<AssetDefinition>, kernelDefs : Vector.<KernelDefinition>) : void
		{
			kernelSequence.create(assetDefs, kernelDefs);
		}
		
		private function onSequenceReady(event : Event) : void
		{
			sendNotification(ApplicationFacade.SEQUENCE_CREATED);
			sendNotification(ApplicationFacade.SET_PREVIEW_IMAGES, kernelSequence.previewables);
			sendNotification(ApplicationFacade.SET_KERNELS, kernelSequence.shaders);
		}
		
		
	}
}